<?php
    header('Content-type:text/json;charset=utf-8');
    require "../connection.php";
    login();
    function login(){
        global $conn;
        $code = $_GET['code'];
        $nick = $_GET['nick'];
        $avatarUrl = $_GET['avatarUrl'];
        $sex = $_GET['sex'];
        //Appid为开发者appid.appSecret为开发者的appsecret,都可以从微信公众平台获取；
        $appid="wxe61fb7cbd427e6c1";
        $appSecret= "42674e00c2627620eb4af6509bae8efd";
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$appSecret."&js_code=".$code."&grant_type=authorization_code";
        $info = get_info($url);
        $json = json_decode($info);
        $arr = get_object_vars($json);
        $openid = $arr['openid'];
        $session_key = $arr['session_key'];
        // 存入数据库
        if($conn){
            $option = array("openid"=>$openid);
            $sql = "select * from user where user_openid='".$openid."'";
            $result = $conn->doSql($sql);
            if($result){
                $result = json_encode($result);
                echo $result;
            }else{
                if($sex == '0'){
                    $sex = 'none';
                }else{
                    $sex = $sex=='1'?'男':'女';
                }
                $options = array("user_nick"=>$nick,"user_openid"=>$openid,"user_session_key"=>$session_key,"user_sex"=>$sex,"user_imgUrl"=>$avatarUrl);
                $result = $conn->insert("user",$options);
                if($result){
                    $arr['user_nick']=$nick;
                    $arr['user_imgUrl']=$avatarUrl;
                    $arr['user_sex']=$sex;
                    echo json_encode($arr);
                }else{
                    $arr['error']="error";
                    echo json_encode($arr);
                }

            }
        }else{
            echo json_encode(array("error"=>"无法链接数据库"));
        }


    }
    function get_info($url){


        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        return $output;
    }

//     "<br />
//     <b>Warning</b>:  file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in <b>/www/admin/localhost_80/wwwroot/admin/wxlogin.php</b> on line <b>15</b><br />
//     <br />
//     <b>Warning</b>:  file_get_contents(https://api.weixin.qq.com/sns/jscode2session?appid=wxe61fb7cbd427e6c1&amp;secret=42674e00c2627620eb4af6509bae8efd&amp;js_code=011c5x000vM1jK16Im300jdV6Z2c5x0Q&amp;grant_type=authorization_code): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in <b>/www/admin/localhost_80/wwwroot/admin/wxlogin.php</b> on line <b>15</b><br />
//     <br />
//     <b>Warning</b>:  get_object_vars() expects parameter 1 to be object, null given in <b>/www/admin/localhost_80/wwwroot/admin/wxlogin.php</b> on line <b>17</b><br />
//     <br />
//     <b>Fatal error</b>:  Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1364 Field 'user_openid' doesn't have a default value' in /www/admin/localhost_80/wwwroot/public/Model.php:143
//     Stack trace:
//     #0 /www/admin/localhost_80/wwwroot/public/Model.php(143): PDO-&gt;exec('insert into use...')
//     #1 /www/admin/localhost_80/wwwroot/public/Model.php(180): MMysql-&gt;_doExec('insert into use...')
//     #2 /www/admin/localhost_80/wwwroot/admin/wxlogin.php(35): MMysql-&gt;insert('user', Array)
//     #3 /www/admin/localhost_80/wwwroot/admin/wxlogin.php(4): login()
//     #4 {main}
//       thrown in <b>/www/admin/localhost_80/wwwroot/public/Model.php</b> on line <b>143</b><br />
//     ""<br />
